// pages/teacher_statistics_detail/teacher_statistics_detail.js
var mm = require("/../js/thenBy.js");   //调用第三方库
Page({

  /**
   * 页面的初始数据
   */
  data: {
    if_show: 0,
    class_name: '',    //课程名
    input_data: '',    //老师名
    input_class: '',  //班级
    things_people_name: '', //请假人
    things_sum: '',   //请假次数
    count: '',       //页面展示数据
    show: false,        //搜索不到时显示
    show_text: '没有相关数据！',       //搜索不到时显示文字
    Akey: '',           //搜索框输入文字暂存变量
    i: 0                //搜索所需循环变量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({   //获取上个页面传过来的teacher_id
      class_name: options.class_name,
      input_data: options.input_data
    })
    this.get_data()
  },

  get_data(){            //获取统计信息
    var that = this
    wx.cloud.callFunction({      //调用云函数
      name: 'count',
      data: {
        name_collection:"overdue_table",//集合名称    
        condition:{//条件
          before_class: that.data.class_name,
          class_teacher: that.data.input_data
        },
        data:{
        }
      },
      success(res){
        //console.log("success",res);     //打印成功回调
        var list = []  //暂时存放数据
        var i = 0  //循环变量
        while(i < res.result.id.length){
          var arr = {}   //一个json
          arr.input_class = res.result.id[i].class
          arr.things_people_name = res.result.id[i].name
          arr.things_sum = res.result.count[i]
          list.push(arr)
          i++
        }
        that.setData({
          count: list
        })
        var m = 0;   //循环变量
        that.data.count.forEach(item => {item.input_classA = ''});
        that.data.count.forEach(item => {item.input_classB = ''});
        that.data.count.forEach(item => {item.input_classC = ''});
        that.data.count.forEach(item => {item.input_classD = ''});
        while(m < that.data.count.length){
          var input_classA = 'count['+m+'].input_classA';
          var input_classB = 'count['+m+'].input_classB';
          var input_classC = 'count['+m+'].input_classC';
          var input_classD = 'count['+m+'].input_classD';
          var A = ((that.data.count[m]).input_class).split(" ");
          that.setData({
            [input_classA]: A[0],
            [input_classB]: A[1],
            [input_classC]: A[2],
            [input_classD]: A[1] + A[2]
          })
          m = m + 1;
        }
        var z = that.data.count;
        z = z.sort(mm.firstBy(function (a, b){     //年级排序  mm.firstBy().thenBy().thenBy() 调用了第三方js库：thenby.js库
          var value1 = a.input_classA;                          //调用库的语句在第二行
          var value2 = b.input_classA;
          return value1 - value2;
        }).thenBy(function (x, y){     //班级排序
          var value3 = x.input_classB;
          var value4 = y.input_classB;
          return value3.localeCompare(value4);
        }).thenBy(function (m, n){     //班级排序
          var value5 = m.input_classC.substr(0,1);
          var value6 = n.input_classC.substr(0,1);
          return value5 - value6;
        }));
        that.setData({
          count: z
        })
        wx.setStorage({     //数据缓存
          key:"count",
          data:that.data.count
        })
        try {
          wx.setStorageSync('count',that.data.count)
        } catch (e) { }
      },//成功的回调
      fail(res){
        console.log("fail",res)
      }
    })
  },
  
  //键盘输入时实时调用搜索方法
  input(e){
    //console.log(e.detail)
    this.search(e.detail.value)
  },

  search(key){         //根据输入框搜索数据
    this.setData({
      Akey: key,
      show: false
    })
    var that = this;
    wx.getStorage({          //获取缓存数据
      key: 'count',
      success (res) {
        //console.log(res.data)
        if (that.data.Akey == ''){            //搜索为空，显示所有
          that.setData({
            count: res.data
          })
        }else{ 
          var arr = [];   //暂时数组，存放数据
          var list = that.data.Akey.split(" ")  //将搜索框中输入内容以空格为分隔符切割
          while(that.data.i < res.data.length){
            if((res.data[that.data.i].input_classA).indexOf(list[0]) >= 0){
              if((list[1] != undefined) && (res.data[that.data.i].input_classD).indexOf(list[1]) >= 0){
                if(((list[2] != undefined) && (res.data[that.data.i].things_people_name).indexOf(list[1]) >= 0) || (list[2] == undefined)){
                  arr.push(res.data[that.data.i])
                  that.setData({
                    i: that.data.i + 1
                  })
                }else{
                  that.setData({
                    i: that.data.i + 1
                  })
                }
              }else if(((list[1] != undefined) && (res.data[that.data.i].things_people_name).indexOf(list[1]) >= 0) || (list[1] == undefined)){
                arr.push(res.data[that.data.i])
                that.setData({
                  i: that.data.i + 1
                })
              }else{
                that.setData({
                  i: that.data.i + 1
                })
              }
            }else if((res.data[that.data.i].input_classD).indexOf(list[0]) >= 0){
              if(((list[1] != undefined) && (res.data[that.data.i].things_people_name).indexOf(list[1]) >= 0) || (list[1] == undefined)){
                arr.push(res.data[that.data.i])
                that.setData({
                  i: that.data.i + 1
                })
              }else{
                that.setData({
                  i: that.data.i + 1
                })
              }
            }else if((res.data[that.data.i].things_people_name).indexOf(list[0]) >= 0){
              arr.push(res.data[that.data.i])
              that.setData({
                i: that.data.i + 1
              })
            }else{
              that.setData({
                i: that.data.i + 1
              })
            }
          }
          //console.log(arr)
          that.setData({
            i: 0
          })
          if (arr.length == 0) {
            that.setData({
              count: [],
              show: true
            })
          } else {
            that.setData({
              count: arr
            })
          }
        }
      }
    })
    try {
      var value = wx.getStorageSync('count')
      if (value) {
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.if_show == 1){
      this.get_data()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      if_show: 1
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})